package edu.java.ishop.controllers;

import java.math.BigDecimal;
import java.security.Principal;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import edu.java.ishop.model.Product;
import edu.java.ishop.model.ProductCategory;
import edu.java.ishop.services.ProductService;

@Controller
@RequestMapping("/product")
public class ProductController {
	
	@Inject
	private ProductService productService;

	@RequestMapping(value="/list", method=RequestMethod.GET)
	public ModelAndView list(Principal principal){
		System.out.println("*************principal:"+principal);
		ModelAndView modelAndView = new ModelAndView();
		List<Product> products = productService.getAll();
		List<ProductCategory> categories = productService.getCategories();
		modelAndView.addObject("products", products);
		modelAndView.addObject("categories",categories );
		modelAndView.setViewName("products");
		return modelAndView;
	}
//	@RequestMapping(value = "/add", method = RequestMethod.POST)
//	public @ResponseBody String add(@ModelAttribute Product product){
//		System.out.println(product.getCategory());
//		productService.saveProduct(product);
//		return "OK";
//	}
	@RequestMapping(value = "/add", method = RequestMethod.POST, consumes ="application/json")
	public @ResponseBody String add(@RequestBody Product product){
		System.out.println(product.getCategory());
		productService.saveProduct(product);
		return "OK";
	}
	@ExceptionHandler(RuntimeException.class)
	public ModelAndView handleException(RuntimeException ex, HttpServletRequest request){
		ModelAndView exModel = new ModelAndView();
		exModel.addObject("cause", ex.getCause().getMessage());
		exModel.setViewName("technicalError");
		return exModel;
	}
	@RequestMapping(value = "/remove" , method =RequestMethod.POST)
	public @ResponseBody String remove(@RequestParam long productId){
		
		productService.deleteProduct(productId);
		return "OK";
	}
	
	@RequestMapping(value = "/edit" , method =RequestMethod.POST, consumes ="application/json")
	public @ResponseBody String edit(@RequestBody Product product
			/*@RequestParam long productId,
			@RequestParam long categoryId,
			@RequestParam String description,
			@RequestParam BigDecimal price,
			@RequestParam String name*/
			){
		productService.update(product);
//		Product product = new Product(productId);
//		ProductCategory category = new ProductCategory(categoryId);
//		//productService.deleteProduct(productId);
//		product.setCategory(category);
//		product.setName(name);
//		product.setPrice(price);
//		product.setDescription(description);
//		//productService.saveProduct(product);
//		productService.update(product);
//		//productService.deleteProduct(product.getId());
		return "OK";
	}
}
